package com.example.shopping_mall.base;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.wifi.aware.AwareResources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shopping_mall.MainActivity;
import com.example.shopping_mall.R;
import com.example.shopping_mall.adapter.ArticleKillAdapter;
import com.example.shopping_mall.adapter.KillDetailAdapter;
import com.example.shopping_mall.application.BaseApplication;
import com.example.shopping_mall.bean.KillDetailBean;
import com.example.shopping_mall.bean.KillDetailPerBean;
import com.example.shopping_mall.bean.SecondBean;
import com.example.shopping_mall.bean.SecondLastBean;
import com.example.shopping_mall.bean.UiKillDetailBean;
import com.example.shopping_mall.bean.UiSecondBean;
import com.example.shopping_mall.http.ShoppingRequest;
import com.example.shopping_mall.utils.ShoppingUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//秒杀详情页
public class KillDetailActivity extends AppCompatActivity {
    private static final String TAG = KillDetailActivity.class.getSimpleName();

    private ShoppingRequest sr;

    //时间
    List<UiKillDetailBean> detailBeanList;
    KillDetailAdapter detailAdapter;
    RecyclerView detailrecyclerView;
    UiKillDetailBean killDetailBean;

    //列表
    SwipeRecyclerView articlerecyclerView;
    List<SecondLastBean> secondLastBeans;
    ArticleKillAdapter adapter;

    SmartRefreshLayout refreshLayout;

    TextView remind;
    ImageView imageView;
    TextView stock;
    Button button;
    SecondLastBean bean;

    int pageSize = 2; // 固定大小
    int startIndex = -1;  // 起始页（从0开始）
    int tempStartIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kill_detail);

        sr = new ShoppingUtils();
        init();
    }

    private void init() {
        refreshLayout=findViewById(R.id.sml);
        detailrecyclerView = findViewById(R.id.kill_detail_recyclerview);
        detailrecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        detailBeanList = new ArrayList<>();
        detailAdapter = new KillDetailAdapter(this);

        articlerecyclerView = findViewById(R.id.article_kill_recyclerview);
        articlerecyclerView.setLayoutManager(new LinearLayoutManager(this));
        secondLastBeans = new ArrayList<>();

        remind=findViewById(R.id.text_view_remind);

        //秒杀专区场次表
        sr.requestPromotionSession(new ShoppingUtils.OnFlashPromotionSessionRequestListener() {
            @Override
            public void OnFlashPromotionSessionRequest(KillDetailBean[] detailBeans) {
                for (int i = 0; i < detailBeans.length; i++) {
                    killDetailBean = new UiKillDetailBean();
                    killDetailBean.setKillTime(detailBeans[i].getName());

                    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
                    try {
                        //将String类型的日期转换为Date类型
                        Date thisTime= dateFormat.parse(new SimpleDateFormat("HH:mm").format(new Date()));
                        Date startTime= dateFormat.parse(detailBeans[i].getStartTime().substring(11,16));
                        Date endTime= dateFormat.parse(detailBeans[i].getEndTime().substring(11,16));

                        if (thisTime.getTime()>endTime.getTime()) {
                            killDetailBean.setKillBuy("已开抢");
                            remind.setText("限时限量，先到先得");
                        }else if (startTime.getTime()<=thisTime.getTime() && thisTime.getTime()<=endTime.getTime()) {
                            killDetailBean.setKillBuy("抢购中");
                            remind.setText("距离本场结束剩余");
                        }else if (thisTime.getTime()<startTime.getTime()){
                            killDetailBean.setKillBuy("即将开始");
                            //remind.setText("设置提醒 好物不错过");
//                            int b=i;
//                            articlerecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//                                @Override
//                                public void onGlobalLayout() {
//                                    try {
//                                        View view=articlerecyclerView.getLayoutManager().findViewByPosition(b);
//                                        stock=view.findViewById(R.id.seconds_kill_details_stock);
//                                        button=view.findViewById(R.id.seconds_kill_details_button);
//
//                                        stock.setText("限量"+secondLastBeans.get(b).getFlashPromotionCount()+"件");
//                                        button.setText("设置提醒");
//                                        //a++;
//                                        Log.d(TAG, "Myposition: "+b);
//                                        //使用完移除监听器
//                                        articlerecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                                    }catch (RuntimeException e){
//                                        e.printStackTrace();
//                                    }
//                                }
//                            });
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    detailBeanList.add(killDetailBean);
                }
                detailAdapter.setDetailBeans(detailBeanList);
                detailrecyclerView.setAdapter(detailAdapter);
            }
        });

        //活动
        sr.requestSmsFlashPromotion(new ShoppingUtils.OnSmsFlashPromotionRequestListener() {
            @Override
            public void OnSmsFlashPromotionRequest(KillDetailPerBean[] killDetailPerBeans) {
                for (int i = 0; i < killDetailPerBeans.length; i++) {
                    int s=killDetailPerBeans[i].getId();



                    if (startIndex == -1)
                    {
                        tempStartIndex = 0;
                    }

                    tempStartIndex = startIndex + pageSize;
                    //每场活动对应的商品
                    sr.requestRelation(killDetailPerBeans[i].getId(),tempStartIndex, tempStartIndex + pageSize - 1,new ShoppingUtils.OnProductRelationRequestListener() {
                        @Override
                        public void OnProductRelationRequest(SecondLastBean[] beans) {
                            for (int j= 0; j < pageSize; j++) {
                                if (s==beans[j].getFlashPromotionId()) {
                                    bean = new SecondLastBean();

                                    startIndex = tempStartIndex;

                                    SecondBean secondBeans = beans[j].getProduct();

                                    secondBeans.setPic(beans[j].getProduct().getPic());
                                    secondBeans.setName(beans[j].getProduct().getName());
                                    secondBeans.setDescription(beans[j].getProduct().getDescription());
                                    bean.setFlashPromotionPrice(beans[j].getFlashPromotionPrice());
                                    secondBeans.setOriginalPrice(beans[j].getProduct().getOriginalPrice());

                                    bean.setProduct(secondBeans);
                                    secondLastBeans.add(bean);

                                }

                                int a=j;
                                articlerecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                                    @Override
                                    public void onGlobalLayout() {
                                        try {
                                            View view=articlerecyclerView.getLayoutManager().findViewByPosition(a);
                                            stock=view.findViewById(R.id.seconds_kill_details_stock);
                                            button=view.findViewById(R.id.seconds_kill_details_button);

                                            if (beans[a].getFlashPromotionCount()>0) {
                                                stock.setText("仅剩"+beans[a].getFlashPromotionCount()+"件");
                                                button.setText("马上抢");
                                            }else if (beans[a].getFlashPromotionCount()<=0) {
                                                stock.setText("被抢光啦");
                                                button.setText("去看看");
                                            }

                                            //使用完移除监听器
                                            articlerecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                                        }catch (RuntimeException e){
                                            e.printStackTrace();
                                        }
                                    }
                                });

//                                sr.requestPromotionSession(new ShoppingUtils.OnFlashPromotionSessionRequestListener() {
//                                    @Override
//                                    public void OnFlashPromotionSessionRequest(KillDetailBean[] detailBeans) {
//                                        for (int i = 0; i < detailBeans.length; i++) {
//                                            killDetailBean = new UiKillDetailBean();
//                                            killDetailBean.setKillTime(detailBeans[i].getName());
//
//                                            detailBeanList.add(killDetailBean);
//                                            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
//                                            try {
//                                                Date thisTime= dateFormat.parse(new SimpleDateFormat("HH:mm").format(new Date()));
//                                                Date startTime= dateFormat.parse(detailBeans[a].getStartTime().substring(11,16));
//                                                Date endTime= dateFormat.parse(detailBeans[a].getEndTime().substring(11,16));
//
//                                                if (thisTime.getTime()>endTime.getTime()) {
//                                                    killDetailBean.setKillBuy("已开抢");
//                                                    articlerecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//                                                        @Override
//                                                        public void onGlobalLayout() {
//                                                            try {
//                                                                View view=articlerecyclerView.getLayoutManager().findViewByPosition(a);
//                                                                stock=view.findViewById(R.id.seconds_kill_details_stock);
//                                                                button=view.findViewById(R.id.seconds_kill_details_button);
//
//                                                                if (beans[a].getFlashPromotionCount()>0) {
//                                                                    stock.setText("仅剩"+beans[a].getFlashPromotionCount()+"件");
//                                                                    button.setText("马上抢");
//                                                                }else if (beans[a].getFlashPromotionCount()<=0) {
//                                                                    stock.setText("被抢光啦");
//                                                                    button.setText("去看看");
//                                                                }
//
//                                                                //使用完移除监听器
//                                                                articlerecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                                                            }catch (RuntimeException e){
//                                                                e.printStackTrace();
//                                                            }
//                                                        }
//                                                    });
//                                                }else if (startTime.getTime()<=thisTime.getTime() && thisTime.getTime()<=endTime.getTime()) {
//                                                    killDetailBean.setKillBuy("抢购中");
//                                                    articlerecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//                                                        @Override
//                                                        public void onGlobalLayout() {
//                                                            try {
//                                                                View view=articlerecyclerView.getLayoutManager().findViewByPosition(a);
//                                                                stock=view.findViewById(R.id.seconds_kill_details_stock);
//                                                                button=view.findViewById(R.id.seconds_kill_details_button);
//
//                                                                if (beans[a].getFlashPromotionCount()>0) {
//                                                                    stock.setText("仅剩"+beans[a].getFlashPromotionCount()+"件");
//                                                                    button.setText("马上抢");
//                                                                }else if (beans[a].getFlashPromotionCount()<=0) {
//                                                                    stock.setText("被抢光啦");
//                                                                    button.setText("去看看");
//                                                                }
//
//                                                                //使用完移除监听器
//                                                                articlerecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                                                            }catch (RuntimeException e){
//                                                                e.printStackTrace();
//                                                            }
//                                                        }
//                                                    });
//                                                }else if (thisTime.getTime()<startTime.getTime()){
//                                                    killDetailBean.setKillBuy("即将开始");
//                                                    //int b=i;
//                                                    articlerecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//                                                        @Override
//                                                        public void onGlobalLayout() {
//                                                            try {
//                                                                View view=articlerecyclerView.getLayoutManager().findViewByPosition(a);
//                                                                stock=view.findViewById(R.id.seconds_kill_details_stock);
//                                                                button=view.findViewById(R.id.seconds_kill_details_button);
//
//                                                                stock.setText("限量"+beans[a].getFlashPromotionCount()+"件");
//                                                                button.setText("设置提醒");
//                                                                //a++;
//                                                                Log.d(TAG, "Myposition: "+a);
//                                                                //使用完移除监听器
//                                                                articlerecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                                                            }catch (RuntimeException e){
//                                                                e.printStackTrace();
//                                                            }
//                                                        }
//                                                    });
//
//                                                }
//                                            } catch (ParseException e) {
//                                                e.printStackTrace();
//                                            }
//                                        }
//                                        detailAdapter.setDetailBeans(detailBeanList);
//                                        detailrecyclerView.setAdapter(detailAdapter);
//
//                                    }
//                                });
                            }
                            adapter = new ArticleKillAdapter(KillDetailActivity.this, secondLastBeans);
                            articlerecyclerView.setAdapter(adapter);
                        }
                    });
                }
            }
        });

        //秒杀时间的条目点击事件
        detailAdapter.setDetailClickListener(new KillDetailAdapter.onKillDetailClickListener() {
            @Override
            public void onKillDetailClick(int position) {
                detailAdapter.setMposition(position);
                detailAdapter.notifyDataSetChanged();

                switch (position){
                    case 0:
//                        sr.requestRelation(2, new ShoppingUtils.OnProductRelationRequestListener() {
//                            @Override
//                            public void OnProductRelationRequest(SecondLastBean[] beans) {
//                                for (int j= 0; j < beans.length; j++) {
//                                    Log.d(TAG, "333333333333: ");
//                                    bean = new SecondLastBean();
//
//                                    SecondBean secondBeans = beans[j].getProduct();
//
//                                    secondBeans.setPic(beans[j].getProduct().getPic());
//                                    secondBeans.setName(beans[j].getProduct().getName());
//                                    secondBeans.setDescription(beans[j].getProduct().getDescription());
//                                    bean.setFlashPromotionPrice(beans[j].getFlashPromotionPrice());
//                                    secondBeans.setOriginalPrice(beans[j].getProduct().getOriginalPrice());
//
//
//                                    int a=j;
//                                    articlerecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//                                        @Override
//                                        public void onGlobalLayout() {
//                                            try {
//                                                View view=articlerecyclerView.getLayoutManager().findViewByPosition(a);
//                                                stock=view.findViewById(R.id.seconds_kill_details_stock);
//                                                button=view.findViewById(R.id.seconds_kill_details_button);
//
//                                                if (beans[a].getFlashPromotionCount()>0) {
//                                                    stock.setText("仅剩"+beans[a].getFlashPromotionCount()+"件");
//                                                    button.setText("马上抢");
//                                                }else if (beans[a].getFlashPromotionCount()<=0) {
//                                                    stock.setText("被抢光啦");
//                                                    button.setText("去看看");
//                                                }
//                                                //a++;
//                                                Log.d(TAG, "Myposition: "+a);
//                                                //使用完移除监听器
//                                                articlerecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                                            }catch (RuntimeException e){
//                                                e.printStackTrace();
//                                            }
//                                        }
//                                    });
//                                    bean.setProduct(secondBeans);
//                                    secondLastBeans.add(bean);
//                                }
//                                adapter = new ArticleKillAdapter(KillDetailActivity.this, secondLastBeans);
//                                articlerecyclerView.setAdapter(adapter);
//                            }
//                        });

                        break;
                    case 1:
                        secondLastBeans.clear();
                        break;
                    case 2:
                        secondLastBeans.clear();
                        break;
                    case 3:
                        secondLastBeans.clear();
                        break;
                    case 4:
                        secondLastBeans.clear();
                        break;
                    case 5:
                        secondLastBeans.clear();
                        break;
                }
                adapter.notifyDataSetChanged();
            }
        });

        //返回主页面
        imageView = findViewById(R.id.kill_detail_View);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(KillDetailActivity.this, MainActivity.class));
            }
        });

}
}
